<template>
  <div class="table"></div>
  <!-- vue3 对 ts 的支持性错误，不知道咋解决 -->
  <Table
    :dataList="dataList"
    :dataCount="dataCount"
    v-bind="contentTableConfig"
    v-model:page="pageInfo"
    @selectionChange="selectionChange"
  >
    <template #headerBtn>
      <el-button v-if="isCreate" @click="handleCreate" type="primary" size="medium">新建用户</el-button>
    </template>

    <template #status="{ row }">
      <el-button plain size="mini" :type="row.enable ? 'success' : 'danger'">
        {{ row.enable ? '启用' : '禁用' }}
      </el-button>
    </template>
    <template #createAt="{ row }">
      <strong>{{ $filters.formatTime(row.createAt) }}</strong>
    </template>
    <template #updateAt="{ row }">
      <strong>{{ $filters.formatTime(row.pdateAt) }}</strong>
    </template>
    <template #columnBtn="{ row }">
      <div class="handle-btns">
        <el-button v-if="isUpdate" @click="handleEdit(row)" icon="el-icon-edit" size="mini" type="text">编辑</el-button>
        <el-button v-if="isDelete" @click="handleDelete(row)" icon="el-icon-delete" size="mini" type="text">
          删除
        </el-button>
      </div>
    </template>

    <template v-for="item in otherPropSlots" :key="item.prop" #[item.slotName]="{ row }">
      <template v-if="item.slotName">
        <slot :name="item.slotName" :row="row"></slot>
      </template>
    </template>
  </Table>
</template>

<script setup lang="ts">
import { ref, watch, computed, defineProps, defineEmits, defineExpose } from 'vue'
import { useStore } from '@/store'
import Table from '@/base-ui/table'
import { usePermission } from '@/hooks/usePermission'

const store = useStore()

const props = defineProps<{
  contentTableConfig: any
  pageName: string
}>()

const isCreate = usePermission(props.pageName, 'create')
const isUpdate = usePermission(props.pageName, 'update')
const isDelete = usePermission(props.pageName, 'delete')
const isQuery = usePermission(props.pageName, 'query')

const pageInfo = ref({ currentPage: 1, pageSize: 10 })
watch(pageInfo, () => getPageData())

const getPageData = (queryData: any = {}) => {
  if (!isQuery) return
  store.dispatch('system/getPageListAction', {
    pageName: props.pageName,
    queryInfo: {
      offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
      size: pageInfo.value.pageSize,
      ...queryData
    }
  })
}
getPageData()

const dataList = computed(() => store.getters[`system/getDataList`](props.pageName))
const dataCount = computed(() => store.getters[`system/getDataCount`](props.pageName))

const selectionChange = ({ id }: any) => {
  console.log(1)
}

// 获取其他的动态插槽名称
const otherPropSlots = props.contentTableConfig?.propList.filter((item: any) => {
  if (item.slotName === 'status') return false
  if (item.slotName === 'createAt') return false
  if (item.slotName === 'updateAt') return false
  if (item.slotName === 'columnBtn') return false
  return true
})

const handleDelete = ({ id }: any) => {
  store.dispatch('system/deletePageDataAction', {
    pageName: props.pageName,
    id
  })
}

const emits = defineEmits(['createBtnClick', 'editBtnClick'])

const handleEdit = (row: any) => {
  emits('editBtnClick', row)
}

const handleCreate = () => {
  emits('createBtnClick')
}

defineExpose({
  getPageData
})
</script>

<style scoped lang="less">
.table {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
